package com.edu.servlet;

import com.edu.bean.Feedback;
import com.edu.service.UserFeedbackService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/feedback")
public class UserFeedbackListServlet extends HttpServlet {
    private UserFeedbackService feedbackService = new UserFeedbackService();
    private static final int PAGE_SIZE = 5;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取分页参数
        String pageStr = request.getParameter("page");
        int page = 1;
        try {
            if (pageStr != null && !pageStr.trim().isEmpty()) {
                page = Integer.parseInt(pageStr);
                if (page < 1) {
                    page = 1;
                }
            }
        } catch (NumberFormatException e) {
            page = 1;
        }

        // 获取数据
        List<Feedback> feedbackList = feedbackService.userGetFeedbackList(page, PAGE_SIZE);
        int totalCount = feedbackService.userGetFeedbackTotalCount();
        int totalPages = (totalCount + PAGE_SIZE - 1) / PAGE_SIZE;

        // 设置属性
        request.setAttribute("feedbackList", feedbackList);
        request.setAttribute("currentPage", page);
        request.setAttribute("totalPages", totalPages);

        // 转发到列表页
        request.getRequestDispatcher("/user/feedback/list.jsp").forward(request, response);
    }
}